package kk.yy.myaccount.main;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.internal.$Gson$Preconditions;
import com.liys.dialoglib.LDialog;

import org.angmarch.views.NiceSpinner;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import kk.yy.myaccount.R;
import kk.yy.myaccount.databinding.FragmentMainBinding;
import kk.yy.myaccount.databinding.FragmentStatisticBinding;
import kk.yy.myaccount.main.adapter.AccountAdapter;
import kk.yy.myaccount.main.service.AccountService;
import kk.yy.myaccount.statistic.StatisticFragment;
import kk.yy.myaccount.utils.MessageData;
import kk.yy.myaccount.view.AccountDialog;

public class MainFragment extends Fragment {

    private FragmentMainBinding mainBinding;
    private static MainFragment mInstance;
    private AccountAdapter accountAdapter;
    public static MainFragment getInstance(){
        if(mInstance == null){
            mInstance=new MainFragment();
        }
        return mInstance;
    }

    private MainFragment(){
        EventBus.getDefault().register(this);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
        mainBinding = mainBinding.inflate(inflater, container, false);
        List data=new ArrayList<>();
        accountAdapter = new AccountAdapter(data);
        mainBinding.mainRv.setLayoutManager(new LinearLayoutManager(getContext()));
        mainBinding.mainRv.setAdapter(accountAdapter);
        mainBinding.outBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
        AccountService accountService=new AccountService();
        accountService.selectData();
        return mainBinding.getRoot();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(MessageData message) {

        if(message.getKey().equals("select")) {
           accountAdapter.getData().clear();
           accountAdapter.getData().addAll((List)message.getValue());
           accountAdapter.notifyDataSetChanged();
        }

    }

    private void showDialog(){
        AccountDialog accountDialog=new AccountDialog(getActivity());
        accountDialog.show();
    }
}

